package net.huashitong.Project.web;

import com.sirdc.modules.core.exceptions.SystemException;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.filter.MessageListFilter;
import net.huashitong.Project.filter.NewProjectsFilter;
import net.huashitong.Project.service.ExportService;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.node.filter.NodeNewschedulesFilter;
import net.huashitong.oversee.entity.ScoreSummary;
import net.huashitong.oversee.filter.GradePointFilter;
import net.huashitong.oversee.filter.ScoreSummaryFilter;
import net.huashitong.oversee.filter.SupervisionTaskFilter;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.filter.BnpUserFilter;
import net.huashitong.user.filter.MessageFilter;
import net.huashitong.user.filter.OnlineFilter;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.ParseException;

import static net.huashitong.node.utile.DateUtil.getNowDate;
import static net.huashitong.utils.UtilsTools.getCurrentDateTime1;
import static net.huashitong.utils.UtilsTools.getLastMonthDateTime;

@Controller
@RequestMapping("/export")
public class ExportExcelCtrl extends JsonBaseController {

    @Autowired
    private ExportService exportService;

    @Override
    protected String getView(String s) {
        return null;
    }

    /**
     * ------------------------导出通讯录----------------
     *
     * @param filter
     * @param reponse
     * @param request
     */
    @ResponseBody
    @RequestMapping(value = "/exportMailList", method = RequestMethod.GET)
    public void exportMailList(@ModelAttribute BnpUserFilter filter, HttpServletResponse reponse, HttpServletRequest request) {
        HSSFWorkbook wb = exportService.getExcelMailListData(filter, filter.getExportHead());
        HSSFSheet sheet = wb.getSheetAt(0);
        int rownum = sheet.getLastRowNum();
        if (rownum == 0) {
            throw new SystemException("没有数据");
        }
        OutputStream out = null;
        try {
            out = reponse.getOutputStream();
            String nowDate = getNowDate();
            String filename = "通讯录" + nowDate + ".xls";
            reponse.setContentType("application/x-msdownload");
            reponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            wb.write(out);
        } catch (Exception e) {

        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * ----------------------导出通讯录-----------------
     *
     * @param
     * @param reponse
     * @param request
     */
    @ResponseBody
    @RequestMapping(value = "/exportOnLineList", method = RequestMethod.GET)
    public void exportMailList(@ModelAttribute OnlineFilter filter, HttpServletResponse reponse, HttpServletRequest request) {
        //默认导出本月
        if (StringUtils.isBlank(filter.getStartDate())) {
            filter.setStartDate(getLastMonthDateTime());
        }
        if (StringUtils.isBlank(filter.getEndDate())) {
            filter.setEndDate(getCurrentDateTime1());
        }
        HSSFWorkbook wb = exportService.getExcelOnLineListData(filter);
        HSSFSheet sheet = wb.getSheetAt(0);
        int rownum = sheet.getLastRowNum();
        if (rownum == 0) {
            throw new SystemException("没有数据");
        }
        OutputStream out = null;
        try {
            out = reponse.getOutputStream();
            String nowDate = getNowDate();
            String filename = "用户登录次数" + nowDate + ".xls";
            reponse.setContentType("application/x-msdownload");
            reponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            wb.write(out);
        } catch (Exception e) {

        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * ----------------------导出节点管理表---------------------
     *
     * @param filter
     * @param reponse
     * @param request
     */
    @ResponseBody
    @RequestMapping(value = "/exportNodeDetail", method = RequestMethod.GET)
    public void exportNodeDetail(@ModelAttribute NewProjectsFilter filter, HttpServletResponse reponse, HttpServletRequest request) throws ParseException {

        HSSFWorkbook wb = exportService.saveExcelNodeManageListData(filter, filter.getExportHead());
        HSSFSheet sheet = wb.getSheetAt(0);
        int rownum = sheet.getLastRowNum();
        if (rownum == 0) {
            throw new SystemException("没有数据");
        }
        OutputStream out = null;
        try {
            out = reponse.getOutputStream();
            String nowDate = getNowDate();
            String filename = "节点管理表" + nowDate + ".xls";
            reponse.setContentType("application/x-msdownload");
            reponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            wb.write(out);
        } catch (Exception e) {

        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * -------------------------导出项目管理表-------------------
     *
     * @param filter
     * @param reponse
     * @param request
     */
    @ResponseBody
    @RequestMapping(value = "/exportProjectDetail", method = RequestMethod.GET)
    public void exportProjectDetail(@ModelAttribute NewProjectsFilter filter, HttpServletResponse reponse, HttpServletRequest request) {
        HSSFWorkbook wb = exportService.getExcelProjectManageListData(filter, filter.getExportHead());
        HSSFSheet sheet = wb.getSheetAt(0);
        int rownum = sheet.getLastRowNum();
        if (rownum == 0) {
            throw new SystemException("没有数据");
        }
        OutputStream out = null;
        try {
            out = reponse.getOutputStream();
            String nowDate = getNowDate();
            String filename = "项目管理表" + nowDate + ".xls";
            reponse.setContentType("application/x-msdownload");
            reponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            wb.write(out);
        } catch (Exception e) {

        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * --------------------------导出考核记录------------------------
     * 导出考核记录
     *
     * @param filter
     * @param reponse
     * @param request
     */
    @ResponseBody
    @RequestMapping(value = "/exportGradePointDetail", method = RequestMethod.GET)
    public void exportGradePointDetail(@ModelAttribute GradePointFilter filter, HttpServletResponse reponse, HttpServletRequest request) {
        HSSFWorkbook wb = exportService.getExcelGradePointListData(filter, filter.getExportHead());
        HSSFSheet sheet = wb.getSheetAt(0);
        int rownum = sheet.getLastRowNum();
        if (rownum == 0) {
            throw new SystemException("没有数据");
        }
        OutputStream out = null;
        try {
            out = reponse.getOutputStream();
            String nowDate = getNowDate();
            String filename = "绩效考核记录表" + nowDate + ".xls";
            reponse.setContentType("application/x-msdownload");
            reponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            wb.write(out);
        } catch (Exception e) {

        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * ---------------------导出绩效考核汇总-------------------------
     *
     * @param filter
     * @param reponse
     * @param request
     */
    @ResponseBody
    @RequestMapping(value = "/exportSummaryPointDetail", method = RequestMethod.GET)
    public void exportSummaryPointDetail(@ModelAttribute ScoreSummaryFilter filter, HttpServletResponse reponse, HttpServletRequest request) {
        HSSFWorkbook wb;
        if (StringUtils.isNotBlank(filter.getStartTime()) || StringUtils.isNotBlank(filter.getEndTime())) {
            //导出有时间段的汇总成绩
            wb = exportService.updateExcelSummaryPointListDataHaveTime(filter, filter.getExportHead());
        }else{
            //导出没有时间段的所有汇总成绩
            wb = exportService.getExcelSummaryPointListData(filter, filter.getExportHead());
        }


        HSSFSheet sheet = wb.getSheetAt(0);
        int rownum = sheet.getLastRowNum();
        if (rownum == 0) {
            throw new SystemException("没有数据");
        }
        OutputStream out = null;
        try {
            out = reponse.getOutputStream();
            String nowDate = getNowDate();
            String filename = "绩效考核记录表" + nowDate + ".xls";
            reponse.setContentType("application/x-msdownload");
            reponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            wb.write(out);
        } catch (Exception e) {

        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * ------------------------导出督查任务-----------------------------
     *
     * @param filter
     * @param reponse
     * @param request
     */
    @ResponseBody
    @RequestMapping(value = "/exportTaskList", method = RequestMethod.GET)
    public void exportTaskList(@ModelAttribute SupervisionTaskFilter filter, HttpServletResponse reponse, HttpServletRequest request) {
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        HSSFWorkbook wb = exportService.getExcelTaskListData(filter, filter.getExportHead(), bnpUser);
        HSSFSheet sheet = wb.getSheetAt(0);
        int rownum = sheet.getLastRowNum();
        if (rownum == 0) {
            throw new SystemException("没有数据");
        }
        OutputStream out = null;
        try {
            out = reponse.getOutputStream();
            String nowDate = getNowDate();
            String filename = "督查任务" + nowDate + ".xls";
            reponse.setContentType("application/x-msdownload");
            reponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            wb.write(out);
        } catch (Exception e) {

        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * ------------------------导出消息列表-----------------------------
     *
     * @param filter
     * @param reponse
     * @param request
     */
    @ResponseBody
    @RequestMapping(value = "/exportMessageList", method = RequestMethod.GET)
    public void exportMessageList(@ModelAttribute MessageFilter filter, HttpServletResponse reponse, HttpServletRequest request) {
        HSSFWorkbook wb = exportService.getExcelMessageListData(filter, filter.getExportHead());
        HSSFSheet sheet = wb.getSheetAt(0);
        int rownum = sheet.getLastRowNum();
        if (rownum == 0) {
            throw new SystemException("没有数据");
        }
        OutputStream out = null;
        try {
            out = reponse.getOutputStream();
            String nowDate = getNowDate();
            String filename = "消息列表" + nowDate + ".xls";
            reponse.setContentType("application/x-msdownload");
            reponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
            wb.write(out);
        } catch (Exception e) {

        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
